/*
 * Email Notify
 * Author: Antonio Nastasi
 * Email: sifcenter (at) gmail.it
 * Version: 0.2
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <ConfigDialog.h>
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QStringList>
#include <KIcon>
#include <KGlobal>
#include <KStandardDirs>
#include <KPageDialog>

ConfigDialog::ConfigDialog()
{
    setFaceType( KPageDialog::List );
    setCaption( "EmailNotify Configure" );
    setButtons( KDialog::Cancel | KDialog::Apply );
    connect( this, SIGNAL( applyClicked() ), this, SLOT( saveConfig()));
    connect( this, SIGNAL( cancelClicked() ), this, SLOT( hide()));
    
    media = Phonon::createPlayer(Phonon::MusicCategory);
    connect(media, SIGNAL(finished()), SLOT(soundFinished()));  
    
    /**** set the page account ****/
    QWidget *widAccount = new QWidget();
    QGridLayout *layoutAccount = new QGridLayout;
    QLabel *label1 = new QLabel("Server:");    
    QLabel *label2 = new QLabel("Port:");
    QLabel *label3 = new QLabel("Username:");
    QLabel *label4 = new QLabel("Password:");
    server_text = new QLineEdit();
    port_spin = new QSpinBox();
    port_spin->setMinimum(110);
    port_spin->setMaximum(65535);
    protocol_combo = new QComboBox();
    protocol_combo->addItem("IMAP",QString());
    protocol_combo->addItem("POP3",QString());
    connect(protocol_combo,SIGNAL(currentIndexChanged(QString)),this,SLOT(protocolIndexChanged(QString)));
    ssl_check = new QCheckBox("SSL");
    connect(ssl_check,SIGNAL(stateChanged(int)),this,SLOT(sslStateChanged(int)));
    login_text = new QLineEdit();
    pass_text = new QLineEdit();
    pass_text-> setEchoMode(QLineEdit::PasswordEchoOnEdit);
    layoutAccount->addWidget(label1,0,0);
    layoutAccount->addWidget(server_text,0,1,1,2);
    layoutAccount->addWidget(label2,1,0);
    layoutAccount->addWidget(port_spin,1,1);
    layoutAccount->addWidget(protocol_combo,1,2);
    layoutAccount->addWidget(ssl_check,1,3);
    layoutAccount->addWidget(label3,2,0);
    layoutAccount->addWidget(login_text,2,1);
    layoutAccount->addWidget(label4,3,0);
    layoutAccount->addWidget(pass_text,3,1);
    widAccount->setLayout(layoutAccount);
    KPageWidgetItem *page = new KPageWidgetItem( widAccount, "Account" );    
    page->setHeader( "Account Page" );
    page->setIcon( KIcon( "user" ) );
    addPage( page );
    /**** set the page settings ****/
    QWidget *widSettings = new QWidget();
    QGridLayout *layoutSettings = new QGridLayout;
    QLabel *label5 = new QLabel("Sound:");
    QLabel *label6 = new QLabel("Inteval(sec):");
    sound_combo = new QComboBox();
    QStringList allFiles;
    KGlobal::dirs()->findAllResources( "sound","",KStandardDirs::Recursive,allFiles );
    sound_combo->addItem("(Nothing)");
    sound_combo->addItems(allFiles);
    play_button = new QPushButton(KIcon("media-playback-start"),"");    
    connect( play_button, SIGNAL(clicked()),this,SLOT( playSound()));
    interval_spin = new QSpinBox();
    interval_spin->setMinimum(5);
    interval_spin->setMaximum(1000);
    layoutSettings->addWidget(label5,0,0);
    layoutSettings->addWidget(sound_combo,0,1);
    layoutSettings->addWidget(play_button,0,2);
    layoutSettings->addWidget(label6,1,0);
    layoutSettings->addWidget(interval_spin,1,1);    
    widSettings->setLayout(layoutSettings);
    page = new KPageWidgetItem( widSettings, "Settings" );    
    page->setHeader( "Settings Page" );
    page->setIcon( KIcon( "configure" ) );
    addPage( page );
    
    config = new KConfig("emailnotifyrc");  
    generalGroup = new KConfigGroup(config,"General"); 
    //loadConfig();    
}

ConfigDialog::~ConfigDialog(){
    //Destruct
}

void ConfigDialog::loadConfig()
{
    bool ok;
    int check;
    config->reparseConfiguration ();
    server_text->setText(generalGroup->readEntry( "Server", QString()));
    port_spin->setValue(generalGroup->readEntry( "Port", QString().toInt(&ok,10)));    
    protocol_combo->setCurrentIndex(protocol_combo->findText(generalGroup->readEntry("Protocol",QString())));
    check = generalGroup->readEntry( "SSL", QString().toInt(&ok,10));
    if(check == 1)
	ssl_check->setCheckState(Qt::Checked);
    else
	ssl_check->setCheckState(Qt::Unchecked);    
    login_text->setText(generalGroup->readEntry( "Login", QString()));    
    pass_text->setText(QByteArray::fromBase64(generalGroup->readEntry( "Password", QString()).toUtf8()));
    interval_spin->setValue(generalGroup->readEntry("Interval",QString().toInt(&ok,10)));
    sound_combo->setCurrentIndex(sound_combo->findText(generalGroup->readEntry( "Sound", QString() )));
}

void ConfigDialog::saveConfig()
{
    generalGroup->writeEntry( "Server", server_text->text() );
    generalGroup->writeEntry( "Port", port_spin->value() );
    generalGroup->writeEntry( "Protocol", protocol_combo->itemText(protocol_combo->currentIndex()));
    if(ssl_check->checkState() == Qt::Checked)
	generalGroup->writeEntry( "SSL", 1 );
    else
	generalGroup->writeEntry( "SSL", 0 );
    generalGroup->writeEntry( "Login", login_text->text() );
    generalGroup->writeEntry( "Password",  pass_text->text().toUtf8().toBase64() );
    generalGroup->writeEntry( "Interval",  interval_spin->value() );
    QString sound_file(sound_combo->itemText(sound_combo->currentIndex()));
    if (sound_file.isEmpty())
	generalGroup->writeEntry( "Sound", "(Nothing)" );
    else
	generalGroup->writeEntry( "Sound", sound_file );
    generalGroup->config()->sync();
    hide();
}

void ConfigDialog::show()
{
    loadConfig();
    play_button->setIcon(KIcon("media-playback-start"));
    KPageDialog::show();  
}

void ConfigDialog::hide()
{    
    media->stop();
    KPageDialog::hide();
}


void ConfigDialog::sslStateChanged(int state)
{   
    /*if(state == Qt::Checked)
	port_spin->setValue(995);
    else
	port_spin->setValue(110);*/
    protocolIndexChanged(protocol_combo->currentText());
}

void ConfigDialog::protocolIndexChanged(QString text)
{
    if(!text.compare("IMAP")){
	if(ssl_check->checkState() == Qt::Checked)
	    port_spin->setValue(993);
	else
	    port_spin->setValue(143);	
    } else if(!text.compare("POP3")){
	if(ssl_check->checkState() == Qt::Checked)
	    port_spin->setValue(995);
	else
	    port_spin->setValue(110);
    }	
}

void ConfigDialog::playSound()
{ 
    QString current(sound_combo->itemText(sound_combo->currentIndex()));
    if(!current.compare("(Nothing)") || current.isEmpty())	
	return;    
    QString file(KGlobal::dirs()->locate("sound",current));   
    media->setCurrentSource(file);
    media->play();
    play_button->setIcon(KIcon("media-playback-stop"));
}

void ConfigDialog::soundFinished()
{
   play_button->setIcon(KIcon("media-playback-start"));
}
